﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TS2015.Models;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using System.Data.Entity;

namespace TS2015.Areas.Admin.Controllers
{
    public class CandidateManagementController : Controller
    {
        TS2015Context db = new TS2015Context();
        // GET: Admin/CandidateManagement
        public ActionResult CandidateList()
        {
            Database.SetInitializer<TS2015Context>(null);
            ViewBag.TbTruong = db.TbTruongs.ToList();
            ViewBag.TbKhuVuc = db.TbKhuVucs.ToList();
            ViewBag.TbXepLoaiHS = db.TbXepLoaiHS.ToList();
            ViewBag.TbDanToc = db.TbDanTocs.ToList();
            ViewBag.TbDoiTuongUuTien = db.TbDoiTuongUuTiens.ToList();
            ViewBag.TbKhoiThi = db.TbKhoiThis.ToList();
            ViewBag.TbHeDaoTao = db.TbHeDaoTaos.ToList();
            ViewBag.TbKhoa = db.TbKhoas.ToList();

            if(!Request.IsAjaxRequest())
                return View();
            
            return PartialView();
        }
        public ActionResult EditingPopup_ReadCandidateList([DataSourceRequest] DataSourceRequest request)
        {
            return Json(db.TbThiSinhs.ToDataSourceResult(request));
        }
        public ActionResult EditingPopup_CreateCandidateList([DataSourceRequest] DataSourceRequest request, TbThiSinh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.TbThiSinhs.Add(data);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_UpdateCandidateList([DataSourceRequest] DataSourceRequest request, TbThiSinh data)
        {
            if (data != null && ModelState.IsValid)
            {
                db.Entry(data).State = EntityState.Modified;
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult EditingPopup_DestroyCandidateList([DataSourceRequest] DataSourceRequest request, TbThiSinh data)
        {
            if (data != null)
            {
                TbThiSinh itemdata = db.TbThiSinhs.First(item => item.MaTS == data.MaTS);
                db.TbThiSinhs.Remove(itemdata);
                db.SaveChanges();
            }
            return Json(ModelState.ToDataSourceResult());
        }
        public ActionResult CreateCandidate(string id)
        {
            TbThiSinh item = new TbThiSinh();
            if(ModelState.IsValid)
            {
                if(id != null)
                {
                    item = db.TbThiSinhs.First(e => e.MaTS == id);
                }
            }
            if (!Request.IsAjaxRequest())
                return View(item);

            return PartialView(item);
        }
        [HttpPost]
        public ActionResult CreateCandidaite(TbThiSinh item)
        {
            if(item != null && ModelState.IsValid)
            {
                
                item.NgayDangKi = DateTime.Now;
                if(item.MaTS != null)
                {
                    db.Entry(item).State = EntityState.Modified;
                    db.SaveChanges();
                }
                else
                {
                    db.TbThiSinhs.Add(item);
                    db.SaveChanges();
                }
                
            }
            TempData["Message"] = "Lưu thành công!";
            return RedirectToAction("CandidaiteList","HighSchoolManagement");
        }
    }
}